//全局websocket
var gloabWs = null;  // 后注释：大哥！单词拼错了 是global
//数据字典
var gloabDict = {notifyType:{HJ:'黄金',YY:'原油',BY:'白银',WH:'外汇'},
			tradeType:{'in':'买入','out':'卖出','avg':'平仓'}
			};

$(function(e){
	// 点击跳转页面和点击效果
	$('.nav-footer > div').on('click','',function(){
		$(this).addClass("footer-nav-active");
		var e = $(this).attr('id');
		var url = '';
		//socket.close();
		if(e=='hall'){
			url="index.html";
		}else  if(e=='notify'){
			url=vidateUser("listMsg.html");
		}else  if(e=='news'){
//			url="market.html?type=1";
//			url="information.html";
			url="http://www.simaipu.net";
		}else  if(e=='my'){
				url=vidateUser("selfMessage.html");
		}else{
			alert('功能正在开发中');
		}
		window.location.href = url;
	});
	
	
	
	var userInfo = getLocalUserInfo();
	function gloabConnect() {
	    var url1 = 'ws://'+websocketDomain+'/tpartner-websocket/websocket/notifyws';
	    var url2 = 'http://'+websocketDomain+'/tpartner-websocket/websocket/notifyws';
	   
	    if ('WebSocket' in window) {
	    	gloabWs = new WebSocket(url1);
	    } else if ('MozWebSocket' in window) {
	    	gloabWs = new MozWebSocket(url1);
	    } else {
	    	gloabWs = new SockJS(url2);
	    }
	//    ws = new SockJS(url2);
	    
	    gloabWs.onopen = function () {
	    	if(userInfo){
	    		var data =  $.extend({_requestType:'userInfo'}, userInfo);
				var userInfoJsonStr = JSON.stringify(data);
				gloabWs.send(userInfoJsonStr);
			} 
	    };
	    // 后加，用于同步 临时用户
	    gloabWs.onopenTmp = function () {
	    	if(userInfoTmp){
	    		var data =  $.extend({_requestType:'userInfoTmp'}, userInfoTmp);
				var userInfoJsonStr = JSON.stringify(data);
				gloabWs.send(userInfoJsonStr);
			} 
	    };
	    gloabWs.onmessage = function (event) {
	    	if(event.data=='ok'){
	    	}else{
		    	//数据派发
		    	var data  = JSON.parse(event.data);
		    	var eventType = data._type;
		    	var d = data;//真正的业务数据
		    	userInfo = getLocalUserInfo();
		    	var ulist = userInfo.ulist;
		    	
		    	var notifyTitle = '';
		    	switch(eventType){
			    	case 'notifyCreated'://房间喊话数据更新
			    		var notifyTitle =  '有新的喊单消息';
			    		break;
			    	case 'notifyUpdated'://房间喊话数据更新
			    		var notifyTitle =  '有修改的喊单消息';
			    		break;
			    	case 'notifyDeleted'://喊单消息删除
			    		var notifyTitle =  '有删除的喊单消息';
			    		break;
			    	case 'notifyFreezed'://喊单消息禁用
			    		var notifyTitle =  '有取消的喊单消息';
			    		break;
			    	case 'notifyUnfreezed'://喊单消息禁用
			    		var notifyTitle =  '有恢复的喊单消息';
			    		break;
		    	}
		    	
		    	var alertTemplate = '<div class="modal fade notify-msg-alert-wrap" >'
								        +'<div class="modal-dialog">'
								        +'<div class="modal-content">'
								            +'<div class="modal-header pro-dia-head">'
								                +'<h4 class="modal-title">'+notifyTitle+'</h4>'
								            +'</div>'
								            +'<div class="modal-body pro-dia-box">'
									            +'<div class="business-notify-msg-content">'
						                        	//
									            +'</div>'
	//							                +'<div class="row">'
	//							                    +'<div class="col-xs-6 business-notify-msg-content">'
	//							                        //
	//							                    +'</div>'
	//							                +'</div>'
								            +'</div>'
								            +'<div class="modal-footer pro-footer-box">'
								                +'<div class="row">'
								                    +'<div class="col-xs-6 no-border" ></div>'
								                    +'<div class="btn btn-inline btn-a col-xs-6 no-border notifyAlertCloseBtn">确定</div>'
								                +'</div>'
								            +'</div>'
								        +'</div>'
								    +'</div>'
								+'</div>'
								;
		    	var $alertTemplate = $(alertTemplate);
		    	$('body').append($alertTemplate);
		    	
		    	var notifyTemplate = '<div class="row list-msg-box business-notify" style="text-align:left;" name="">'
												+'<div class="col-xs-12">'
												+'<div class="tab-border">'
													+'<table class="table">'
														+'<tbody>'
															+'<tr>'
																+'<td>'
																+'<span class="business-category"></span>'
																+'<span class="business-tradeType"></span>'
																+'</td>'
																+'<td>'
																+'<span>止损:</span>'
																+'<span class="business-lossPt"></span>'
																+'</td>'
															+'</tr>'
															+'<tr>'
																+'<td  width="50%">'
																+'<span class="strong-style business-pt"></span>'
																+'</td>'
																+'<td class="top-vertical">'
																+'<span>止盈:</span>'
																+'<span class="business-profitPt"></span>'
																+'</td>'
															+'</tr>'
															+'<tr>'
																+'<td>'
																+'<span>仓位:</span>'
																+'<span class="business-fs"></span>'
																+'</td>'
																+'<td >'
																+'<span class="mar-count">时间:</span>'
																+'<span class="mar-count business-publishTime"></span>'
																+'</td>'
															+'</tr>'
															+'<tr>'
																+'<td colspan="2">'
																+'<span>备注:</span>'
																+'<span class="business-remark"></span>'
																+'</td> '
															+'</tr>'
													+'</tbody>'
												+'</table>'
											+'</div>'
										+'</div>'
									+'</div>'
									;
		    	var $notify = $(notifyTemplate);
		    	
		    	var tradeTypeClass = '';
	    		if(d.tradeType == 'in'){
					tradeTypeClass = "buy-icon-box";
				}else{
					tradeTypeClass="open-sell-icon";
				}
	    		var categoryType = gloabDict.notifyType[d.categoryCode];
				var tradeType = gloabDict.tradeType[d.tradeType];
	//			$notify.attr('name',d.id);
				$notify.find('.business-category').html(categoryType);
				$notify.find('.business-tradeType').addClass(tradeTypeClass).html(tradeType);
				$notify.find('.business-lossPt').html(d.lossPt);
				$notify.find('.business-pt').html(d.pt);
				$notify.find('.business-entrust').html(d.entrust);
				$notify.find('.business-remark').html(d.remark);
				var publishTime = new Date(d.publishTime).format("yyyy-MM-dd");
				$notify.find('.business-publishTime').html(publishTime);
				$notify.find('.business-profitPt').html(d.profitPt);
				$notify.find('.business-fs').html(d.fs);
	    		
				$alertTemplate.find('.business-notify-msg-content').append($notify);
				
				//关闭弹框事件
				$alertTemplate.find('.notifyAlertCloseBtn').click(function(){
					var parent = $(this).parents('.notify-msg-alert-wrap').get(0);
					var $parent = $(parent);
					
					$parent.modal('hide');
					setTimeout(function(){
						$parent.remove();
					},1000);
				});
				$alertTemplate.bind('contextmenu',function(){
					var $alert = $(this);
					setTimeout(function(){
						$alert.remove();
					},1000);
				});
				
				$alertTemplate.modal({
		            width:400,
		            title:'',
		            dismiss:true,
		            showTitle:true
		        });
	    	
	    	}
	    };
	    gloabWs.onclose = function (event) {
	    };
	};

	if(gloabWs){
		goloabWs.close();
	}
	gloabConnect();

});

function vidateUser(url){
	var userInfo = getLocalUserInfo();
    //判断如果用户未登录，则跳转到登录界面
    if(userInfo == null || !userInfo._loginStatus){
    		return   'login.html?redirectUrl=selfMessage.html';
	} else {
			$('#tel').val(userInfo.mphone);
			$('#email').val(userInfo.email);
			$('#nick').val(userInfo.nick);
			return url;
	}
}
